<?php
class SchedulerController extends Zend_Controller_Action
{
	public function init()
	{
		Application_Helper_Authenticate::checkLogin($this);
	}

	public function indexAction()
	{
		$scheduler = new Application_Model_DbTable_Scheduler();

		$list = $scheduler->getAllInfo();

		$this->view->list = $list;
	}

	public function addAction()
	{
		$form = new Application_Form_Scheduler();
		$this->view->form = $form;

		$scheduler = new Application_Model_DbTable_Scheduler();

		if ($this->getRequest()->isPost())
		{
			$formData = $this->getRequest()->getPost();

			$sendTime = $formData['sending_time'];

			if ($form->isValid($formData) && $this->_checkTime($sendTime) )
			{
				$token = $form->getValue('token');
				$myToken = Application_Helper_Session::get('token');

				if($token == $myToken)
				{
					Application_Helper_Session::remove('token');
					$formData['created_user'] =
					Application_Helper_Authenticate::getUid();

					$formData['created_time'] = time();
					$formData['sending_time'] = $sendTime;
					$formData['list_ids'] = implode(',',
							$formData['list_ids']);

					$scheduler->addScheduler($formData);

					$this->_helper->redirector('index');
				}
				else
				{
					$this->_helper->redirector('index');
				}
			}
			else
			{
				echo '<pre>';
				print_r($formData);
				echo '</pre>';
				$form->populate($formData);
			}
		}
		else
		{
			$values = array();

			$token = time();

			Application_Helper_Session::set('token', $token);

			$values['token'] = $token;

			$form->populate($values);
		}
	}

	public  function deleteAction()
	{
		if ($this->getRequest()->isPost()) {

			$del = $this->getRequest()->getPost('del');

			if ($del == 'Yes') {

				$id = $this->getRequest()->getPost('id');

				$list = new Application_Model_DbTable_Scheduler();
				$list->deleteScheduler($id);
			}

			$this->_helper->redirector('index');

		} else {

			$id = $this->_getParam('id', 0);

			$list = new Application_Model_DbTable_Scheduler();
			$listContact = new Application_Model_DbTable_List();

			$list = $list->getScheduler($id);
			
			$a =  explode(',',$list['list_ids']);
				
			$list['list_ids'] = array();
			
			$a = $listContact->getSomeList($a);
				
			foreach ($a as $index => $value)
			{
				$list['list_ids'][$index] =  $value;
			}
			
			$list['list_ids'] = implode(', ', $list['list_ids']);
			
			$list['sending_time'] = date('Y-m-d',$list['sending_time']);
			
			$this->view->list = $list;
		}
	}

	public function editAction()
	{
		$form = new Application_Form_Scheduler();
		$this->view->form = $form;

		$scheduler = new Application_Model_DbTable_Scheduler();
		$id = $this->_getParam('id', 0);

		$value = $scheduler->getScheduler($id);

		if ($this->getRequest()->isPost())
		{
			$formData = $this->getRequest()->getPost();

			$sendTime = $formData['sending_time'];

			if ($form->isValid($formData) && $this->_checkTime($sendTime) )
			{
				$token = $form->getValue('token');
				$myToken = Application_Helper_Session::get('token');

				if($token == $myToken)
				{
					Application_Helper_Session::remove('token');

					$formData['sending_time'] = $sendTime;
						
					$formData['list_ids'] = implode(',',
							$formData['list_ids']);

					$formData['created_user'] = $value['created_user'];
					
					$formData['created_time'] = $value['created_time'];
					
					$scheduler->updateScheduler($id, $formData);

					$this->_helper->redirector('index');
				}
				else
				{
					$this->_helper->redirector('index');
				}
			}
			else
			{
				$form->populate($formData);
			}
		}
		else
		{
			$token = time();

			Application_Helper_Session::set('token', $token);

			$value['token'] = $token;
				
			$value['sending_time'] = date('Y-m-d',$value['sending_time']);
				
			$value['created_time'] = date('Y-m-d',$value['created_time']);
				
			$value['list_ids'] = explode(',', $value['list_ids']);

			$form->populate($value);
		}
	}

	private function _checkTime(& $time)
	{
		$pattern = '/((\d\d\d\d)|(\d\d))-((\d\d)|\d)-((\d\d)|\d)/';
		if (preg_match($pattern, $time))
		{
			$time = explode('-', $time);

			if ((int)$time[0] >  2100 ||
					(int)$time[1] > 12 ||
					(int)$time[2] > 30)
				return false;

			if($time[0] < 100)
				$time[0] = 2000 + $time[0];

			$time = mktime(0, 0, 0, $time[1],
					$time[2], $time[0]);

			if ($time < time())
				return false;

			return true;
		}
		else
		{
			return false;
		}
	}

}